package com.amzing.dynamicrulesshardingjdbc.service.impl;

import com.amzing.dynamicrulesshardingjdbc.entity.Order;
import com.amzing.dynamicrulesshardingjdbc.entity.OrderItem;
import com.amzing.dynamicrulesshardingjdbc.mapper.OrderItemMapper;
import com.amzing.dynamicrulesshardingjdbc.mapper.OrderMapper;
import com.amzing.dynamicrulesshardingjdbc.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public void select(long tenantId) {
        orderMapper.selectOrderItems(tenantId);
        orderItemMapper.selectOrders(tenantId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void transaction() {
        int tenantId = 2;

        Order order = new Order();
        order.setStatus("1");
        order.setAddressId(3L);
        order.setUserId(3);
        order.setTenantId(2L);
        this.save(order);

        try {
            OrderItem item = new OrderItem();
            item.setOrderId(3L);
            item.setStatus("3L");
            item.setUserId(3);
            item.setTenantId(2L);
            orderItemMapper.insert(item);
//            if(true){
//                throw new RuntimeException("transaction test");
//            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
